package com.domain.controller;

import com.domain.common.Comments;
import com.domain.common.Result;
import com.domain.pojo.SmsCode;
import com.domain.service.H5Service;
import com.domain.service.RedisService;
import com.domain.service.SmsCodeService;
import com.domain.service.SmsService;
import com.domain.utils.NotNullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/h5")
public class H5Controller {
    @Autowired
    private H5Service h5Service;

    @Autowired
    private SmsService smsService;


    @Autowired
    private RedisService redisService;


    /**
     * 获取短信验证码(阿里短信)
     *
     * @param phoneNum //手机号码
     */
    @RequestMapping(value = "/getsmsCode", method = RequestMethod.GET)
    @ResponseBody
    public Result getsmsCode(String phoneNum) {
        try {
            return smsService.sendMessage(phoneNum, "7676");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("短信验证码获取失败");
        }
    }

    /**
     * 获取帮忙详情
     *
     * @param helpUserId
     * @param publishId
     * @return
     */
    @RequestMapping(value = "/getH5PublishDetail", method = RequestMethod.GET
    )
    @ResponseBody
    public Result getH5PublishDetail(String helpUserId, String publishId, String helpType) {
        try {
            return h5Service.getH5PublishDetail(helpUserId, publishId, helpType);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("操作失败");
        }
    }

    /**
     * h5注册
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/h5Regester", method = RequestMethod.GET)
    @ResponseBody
    public Result h5Regester(String phoneNumber, String smsCode, String helpUsersId) {
        try {
            if (!smsService.checkIsCorrectCode(phoneNumber, smsCode)) {
                return Result.publicResult(Comments.SMSCODE_FAIL, "验证码错误");
            } else if (redisService.getValue(phoneNumber) == null) {
                return Result.publicResult(Comments.SMSCODE_CHECK, "验证码已过期！");
            }
            return h5Service.h5Regester(phoneNumber, helpUsersId, smsCode);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

}
